<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:text="${userType == 'user' ? '新增学生' : '新增教师'}"></title><!--但是新增教师功能还未完善-->
    <style>
        body {
            font-family: "Helvetica Neue", sans-serif;
            margin: 30px;
            background-color: #f9f9f9;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        form {
            max-width: 500px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 6px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        label {
            display: block;
            margin: 12px 0 6px;
            font-weight: 600;
        }

        input, select {
            width: 100%;
            padding: 8px 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        button {
            margin-top: 20px;
            width: 100%;
            padding: 10px;
            background-color: #2c3e50;
            border: none;
            color: white;
            font-size: 16px;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background-color: #1a2733;
        }
    </style>
    <script>
        function submitForm(event) {
            event.preventDefault(); /*阻止表单默认提交*/
            const formData = {
                username: document.getElementById('username').value.trim(),
                password: document.getElementById('password').value,
                phone: document.getElementById('phone').value.trim(),
                name: document.getElementById('name').value.trim(),
                userType: document.getElementById('userType').value,
                className: document.getElementById('className') ? document.getElementById('className').value : null
            };


            fetch('/user/add', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(formData)
            }).then(res => res.json())
                .then(data => {
                    if (data.code === 0) {
                        alert("新增成功");
                        window.location.href = '/user';
                    } else {
                        alert("新增失败：" + data.message);
                    }
                });
            return false; // 阻止表单默认提交
        }
        /*拦截表单提交，收集表单数据，通过fetch发送 POST 请求到后端接口   /user/add  。*/
    </script>
</head>
<body>
<h1 th:text="${userType == 'user' ? '新增学生' : '新增教师'}">新增用户</h1>
<form onsubmit="submitForm(event)">
    <label for="username" th:text="${userType == 'user' ? '学号' : '编号'}"></label>
    <input type="text" id="username" name="username" required>
    <input type="hidden" id="userType" name="userType" th:value="${userType}">



    <label for="password">密码</label>
    <input type="text" id="password" name="password" required>

    <label for="phone">手机号</label>
    <input type="text" id="phone" name="phone">

    <!-- ${userType == 'user'}   是一个表达式，用于判断userType的值是否等于字符串'user'  。学生和教师的表单可能需要不同的字段。例如，学生可能需要填写“班级”，而教师可能不需要。-->

    <div th:if="${userType == 'user'}">

        <label for="className">班级</label>
        <select id="className" name="className">
            <option value="">请选择班级</option>
            <option value="应急管理">应急管理</option>
            <option value="应急技术与管理">应急技术与管理</option>
            <option value="应用心理学">应用心理学</option>
            <option value="智慧应急">智慧应急</option>
            <!-- 可以根据需要动态渲染 -->
        </select>
    </div>


    <label for="name">真实姓名</label>
    <input type="text" id="name" name="name">

    <button type="submit">保存</button>
</form>
</body>
</html>
